package cn.exrick.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.exrick.mapper.TbItemSkuCatMapper;
import cn.exrick.pojo.TbItemSkuCat;
import cn.exrick.service.ItemSkuCatService;
import cn.exrick.utils.mybatis.Criteria;

@Service
public class ItemSkuCatServiceImpl extends BaseServiceImpl<TbItemSkuCat, TbItemSkuCatMapper> implements ItemSkuCatService {
    private final static Logger log = LoggerFactory.getLogger(ItemSkuCatServiceImpl.class);

    @Override
    public Logger getLogger() {
        return log;
    }

    public Criteria createCriteria(Map<String, Object> params) {
        Criteria criteria = createCriteria();
        if (params.get("name") != null) {
            criteria.andFieldEqualTo("name", (String) params.get("name"));
        }
        if (params.get("nameLike") != null) {
            criteria.andFieldLike("name", (String) params.get("nameLike"));
        }
        return criteria;
    }

    @Autowired
    TbItemSkuCatMapper tbItemSkuCatMapper;

    @Override
    public TbItemSkuCatMapper getMapper() {
        return tbItemSkuCatMapper;
    }

    @Override
    public boolean exist(String name) {
        return findByName(name) != null;
    }

    public TbItemSkuCat findByName(String name) {
        List<TbItemSkuCat> list = findListByCriteria(createCriteria(new HashMap<String, Object>() {{
            this.put("name", name);
        }}));
        if (list == null || list.size() <= 0) {
            return null;
        } else {
            return list.get(0);
        }
    }
}
